<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

use app\common\model\UserAccount;
use app\common\model\UserAccountLog;
use think\Db;

/**
 * 管理员控制器
 */
class Recharge extends AdminBase
{


    
    /**
     * 后台充值列表
     */
    public function memberList()
    {

        
        $this->assign('list', $this->logicMember->getMemberList($this->param,'a.*','a.create_time desc',50));
        $this->assign('true',true);
        return $this->fetch('member/member_list');
    }


    /**
     * 后台充值页面
     */
    public function recharge()
    {

        if(request()->isPost()){
            $this->jump($this->logicMember->recharge($this->param));

        }

        $this->assign('wallet', \app\common\model\Wallet::all(['status'=>1,'is_virtual'=>1,'is_currency'=>1]));
        return $this->fetch();
    }

    /**
     * 线下法币充值记录页面
     */
    public function  legal_currency_recharge_log()
    {
        $this->param['paytype']=1;
        $this->assign('list', $this->logicRecharge->getRechargeList($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }

    /**
     * 线下钱包充值记录页面
     */
    public function  wallet_currency_recharge_log()
    {
        $this->param['paytype']=2;
        $this->assign('list', $this->logicRecharge->getRechargeList($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('wallet', \app\common\model\Wallet::all(['is_currency'=>1,'status'=>1,'is_virtual'=>1]));
        $this->assign('param',$this->param);
        return $this->fetch();
    }


    /**
     * 共生池充值记录页面
     */
    public function  share_balance_recharge_log()
    {

        if(isset($this->param['is_recharge'])){
            return $this->fetch('share_recharge');
        }
        IS_POST &&   $this->jump($this->logicRecharge->share_recharge($this->param));
        $this->assign('list', $this->logicRecharge->getShareRechargeList($this->param,'a.*','a.create_time desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }

    /**
     * 应用余额转账记录页面
     */
    public function  app_balance_transfer_log()
    {

        if(isset($this->param['is_transfer'])){
            $this->jump($this->logicRecharge->app_transfer_check($this->param));
         }

        $this->assign('list', $this->logicRecharge->getAppTransferList($this->param,'a.*,c.username,c.mobile','a.state asc,a.id desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }
    /**
     * 划转记录页面
     */
    public function  eth_transfer_log()
    {
        $this->assign('list', $this->logicRecharge->geEthTransferList($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('wallet', \app\common\model\Wallet::all(['is_currency'=>1,'status'=>1,'is_virtual'=>1]));
        $this->assign('param',$this->param);
        return $this->fetch();
    }

    /**
     * 充值审核
     */
    public function recharge_check(){
        $this->jump($this->logicRecharge->recharge_check($this->param));
    }

    /**
     * 手动充值记录页面
     */
    public function admin_recharge_log()
    {

        $this->assign('list', $this->logicRecharge->getAdminRechargeList($this->param,'a.*,c.username,is_company,mobile','a.create_time desc',50));
        $this->assign('wallet', \app\common\model\Wallet::all(['status'=>1,'is_virtual'=>1]));
        $this->assign('param',$this->param);
        return $this->fetch();
    }

    /**
     * 充值记录导出
     */
    public function exportAdminRechargeList()
    {



        $this->logicRecharge->exportAdminRechargeList($this->param);
    }

    /**
     * 闪兑钱包交易记录页面
     */
    public function trade_log()
    {

        $this->assign('list', $this->logicRecharge->getTradeList($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('wallet', \app\common\model\Wallet::all(['is_currency'=>1,'status'=>1,'is_virtual'=>1]));
        $this->assign('param',$this->param);
        return $this->fetch();
    }

    /**
     * 转账记录页面
     */
    public function transfer_log()
    {

        $this->assign('list', $this->logicRecharge->getTransferList($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('param',$this->param);
        $this->assign('wallet',\app\common\model\Wallet::all(['status'=>1,'is_virtual'=>1,'is_currency'=>1]));
        return $this->fetch();
    }

    /**
     *  转账记录导出
     */
    public function exportTransferList()
    {


        $this->logicRecharge->exportTransferList($this->param);
    }

    /**
     * 审核充值
     */
    public function  setStatus(){
        $this->jump($this->logicRecharge->setStatus($this->param));

    }



    /**
     * 提币审核
     */
    public function  withdrawal_check(){
        $this->jump($this->logicRecharge->withdrawal_check($this->param));

    }



    /**
     * 提币记录页面
     */
    public function withdrawal_log()
    {
        $this->param['type']=20;

        $this->assign('list', $this->logicRecharge->withdrawal_log($this->param,'a.*,c.username,c.mobile,c.is_company,c.is_king,c.is_shop','a.create_time desc',50));
        $this->assign('wallet', \app\common\model\Wallet::all(['is_currency'=>1,'status'=>1,'is_virtual'=>1]));
        $this->assign('param',$this->param);
        return $this->fetch();
    }


    /**
     * 总包充值金豆记录页面
     */
    public function charge_peas_log()
    {
        $this->param['type']=21;
        $this->param['state']=1;
        $this->assign('list', $this->logicRecharge->withdrawal_log($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }


    /**
     * 余额流水页面
     */
    public function account_log()
    {

        $this->assign('list', $this->logicRecharge->account_log($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('wallet', \app\common\model\Wallet::all(['status'=>1,'is_virtual'=>1]));
        $this->assign('param',$this->param);
        return $this->fetch();
    }

    /**
     * 总包流水页面
     */
    public function account_log4()
    {
        $this->param['wallet_id']=1;
        $this->assign('list', $this->logicRecharge->account_log($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('wallet', \app\common\model\Wallet::all(['status'=>1,'is_virtual'=>1]));
        $this->assign('param',$this->param);
        return $this->fetch();
    }


    /**
     * 理财包流水页面
     */
    public function account_log5()
    {
        $this->param['wallet_id']=3;
        $this->assign('list', $this->logicRecharge->account_log($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('wallet', \app\common\model\Wallet::all(['status'=>1,'is_virtual'=>1]));
        $this->assign('param',$this->param);
        return $this->fetch();
    }



    /**
     * 静态释放页面
     */
    public function account_log2()
    {
        $this->param['type']=9;
        $this->param['wallet_id']=1;
        $this->assign('list', $this->logicRecharge->account_log2($this->param,'a.*,c.username','a.id desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }

    /**
     *  静态释放记录导出
     */
    public function exportAccountList2()
    {

        $this->param['type']=9;
        $this->param['wallet_id']=1;
        $this->logicRecharge->exportAccountList2($this->param);
    }

    /**
     * 动态页面
     */
    public function account_log3()
    {

        $this->assign('list', $this->logicRecharge->account_log3($this->param,'a.*,c.username','a.id desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }

    /**
     * 动态记录导出
     */
    public function exportAccountList3()
    {

        $this->logicRecharge->exportAccountList3($this->param);
    }

    /**
     * 交易审核
     */
    public function  trade_check(){
        $this->jump($this->logicRecharge->trade_check($this->param));

    }

    /**
     * 充值线下充值记录导出
     */
    public function exportRechargeList()
    {


        $this->param['paytype']=1;
        $this->logicRecharge->exportRechargeList($this->param);
    }


    /**
     * 充值线下钱包充值记录导出
     */
    public function exportWalletRechargeList()
    {


        $this->param['paytype']=2;
        $this->logicRecharge->exportRechargeList($this->param);
    }


    /**
     * 划转记录导出
     */
    public function exporEthTransferList()
    {


        $this->logicRecharge->exporEthTransferList($this->param);
    }

    /**
     * 提币记录导出
     */
    public function exportWithdrawalletList()
    {

        $this->param['type']=20;
        $this->logicRecharge->exportWithdrawalletList($this->param);
    }

    /**
     * 总包充值金豆记录导出
     */
    public function exportChargePeastList()
    {
        $this->param['type']=21;
        $this->param['state']=1;
        $this->logicRecharge->exportWithdrawalletList($this->param);
    }











    /**
     * 转换记录
     */
    public function all_exchange_log()
    {
        $this->assign('list', $this->logicRecharge->all_exchange_log($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }
    /**
     * 盈利包转回总钱包记录导出
     */
    public function exportAllExchangeList()
    {


        $this->logicRecharge->exportAllExchangeList($this->param);
    }

    /**
     * EC 提取到钱包记录导出
     */
    public function exportWithdrawalList1()
    {


        $this->logicRecharge->exportWithdrawalList1($this->param);
    }


    /**
     * 释放记录页面
     */
    public function release_log()
    {

        $this->assign('list', $this->logicRecharge->release_log($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }

    /**
     * 释放记录导出
     */
    public function exportReleaseList()
    {



        $this->logicRecharge->exportReleaseList($this->param);
    }



    /***
     * 购买课程记录
     */
    public function course_log()
    {

        $this->assign('list', $this->logicRecharge->course_log($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }


    /**
     * 导出购买课程列表
     */
    public function exportCourseList()
    {



        $this->logicRecharge->exportCourseList($this->param);
    }


    /***
     * 购买课程记录
     */
    public function reward_log()
    {

        $this->assign('list', $this->logicRecharge->reward_log($this->param,'a.*,c.username','a.create_time desc',50));
        $this->assign('param',$this->param);
        return $this->fetch();
    }


    /**
     * 导出打赏记录列表
     */
    public function exportRewardList()
    {



        $this->logicRecharge->exportRewardList($this->param);
    }


    /** 
     * 财务收益统计
    */
    public function finance_statistics(){
        $list = array();
        if(!empty($this->param['date1']) && !empty($this->param["date2"])){
            $start_time = strtotime($this->param['date1']);
            $end_time = strtotime($this->param["date2"]);
            $time = $start_time;
            while($time<$end_time+24*60*60){

                $list[] = ["date"=>date("Y-m-d",$time)];
                $time+=24*60*60;
            }
            
            foreach($list as $k=>$v){
                //会员卡收益
                $list[$k]['member_card_profit'] = (Db::name("user_account_log")->where(['type'=>14,'create_time'=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")-
                Db::name("user_account_log")->where(['type'=>10,'create_time'=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number"))??0;
                //业务流水收益
                $list[$k]['business_stream_profit'] = Db::name("games_log")->where(["type"=>1,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("king_number")
                // +Db::name("games_log")->where(["type"=>6,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                +Db::name("bets_log")->where(["create_time"=>["state"=>["in",[2,3]],'between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("money")*(1998-1960)/10000??0;
                //手续费支出
                $list[$k]['poundage_payment'] = Db::name("shop_account_log")->where(["type"=>3,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                //交易员收益
                $list[$k]['traders_profit'] = Db::name("investment_bonus_log")->where(["bonus"=>['gt',0],"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("bonus")??0;
               //贡献奖
                $list[$k]['contribution_reward'] = Db::name("user_account_log")->where(["type"=>4,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                //共生池收入
                $list[$k]['more_money_add'] = Db::name("company_account_log")->where(['type'=>2,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                //共生池支出
                $list[$k]['more_money_reduce'] = Db::name("company_account_log")->where(['type'=>3,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                //对撞共生池拨出
                $list[$k]['dig_money'] = Db::name("company_account_log")->where(['type'=>4,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                //总盈亏
                $list[$k]['total'] =  $list[$k]['member_card_profit'] + $list[$k]['business_stream_profit'] + $list[$k]['traders_profit'] - $list[$k]['poundage_payment'] - $list[$k]['contribution_reward'] + $list[$k]['more_money_add']- $list[$k]['more_money_reduce'] - $list[$k]['dig_money'];
            }
        }else{
            $end_time = time();
            $start_time = time()-30*24*60*60;
            $time = $start_time;
            while($time<$end_time+24*60*60){
                $list[] = ["date"=>date("Y-m-d",$time)];
                $time+=24*60*60;
            }
            foreach($list as $k=>$v){
                //会员卡收益
                $list[$k]['member_card_profit'] = (Db::name("user_account_log")->where(['type'=>14,'create_time'=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")-
                Db::name("user_account_log")->where(['type'=>10,'create_time'=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number"))??0;
                //业务流水收益
                $list[$k]['business_stream_profit'] = Db::name("games_log")->where(["type"=>1,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("king_number")
                // +Db::name("games_log")->where(["type"=>6,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                +Db::name("bets_log")->where(["state"=>["in",[2,3]],"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("money")*(1998-1960)/10000??0;
               //手续费支出
                $list[$k]['poundage_payment'] = Db::name("shop_account_log")->where(["type"=>3,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;   
               //交易员收益
                $list[$k]['traders_profit'] = Db::name("investment_bonus_log")->where(["bonus"=>['gt',0],"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("bonus")??0;
                //贡献奖
                $list[$k]['contribution_reward'] = Db::name("user_account_log")->where(["type"=>4,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                //共生池收入
                $list[$k]['more_money_add'] = Db::name("company_account_log")->where(['type'=>2,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                //共生池支出
                $list[$k]['more_money_reduce'] = Db::name("company_account_log")->where(['type'=>3,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                //对撞共生池拨出
                $list[$k]['dig_money'] = Db::name("company_account_log")->where(['type'=>4,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("number")??0;
                //总盈亏
                $list[$k]['total'] =  $list[$k]['member_card_profit'] + $list[$k]['business_stream_profit'] + $list[$k]['traders_profit'] - $list[$k]['poundage_payment'] - $list[$k]['contribution_reward'] + $list[$k]['more_money_add']- $list[$k]['more_money_reduce'] - $list[$k]['dig_money'];
            }
        }
        $list = array_reverse($list);
        $this->assign("list",$list);
        return $this->fetch();
    }

    public function company_statistics(){
        $list = array();
        if(!empty($this->param['date1']) && !empty($this->param["date2"])){
            $start_time = strtotime($this->param['date1']);
            $end_time = strtotime($this->param["date2"]);
            $time = $start_time;
            while($time<$end_time+24*60*60){

                $list[] = ["date"=>date("Y-m-d",$time)];
                $time+=24*60*60;
            }
            
            foreach($list as $k=>$v){
                $list[$k]['buy_amount'] = Db::name("bets_log")->where(["create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("money")??0;
                $list[$k]['distribute_amount'] = Db::name("bets_log")->where(["state"=>2,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("reward_money")??0;
                $list[$k]['profit_amount'] = $list[$k]['buy_amount'] - $list[$k]['distribute_amount'];
                
            }
        }else{
            $end_time = time();
            $start_time = time()-30*24*60*60;
            $time = $start_time;
            while($time<$end_time+24*60*60){
                $list[] = ["date"=>date("Y-m-d",$time)];
                $time+=24*60*60;
            }
            foreach($list as $k=>$v){
                $list[$k]['buy_amount'] = Db::name("bets_log")->where(["create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("money")??0;
                $list[$k]['distribute_amount'] = Db::name("bets_log")->where(["state"=>2,"create_time"=>['between',[strtotime($list[$k]['date']),strtotime($list[$k]['date'])+24*60*60]]])->sum("reward_money")??0;
                $list[$k]['profit_amount'] = $list[$k]['buy_amount'] - $list[$k]['distribute_amount'];
                
            }
        }
        $list = array_reverse($list);
        $this->assign("list",$list);
        return $this->fetch();
    }




}
